layui.use(['form', 'http', 'public'], function () {
    onLoad();
});

function onLoad() {
    let id = layui.public.GetQuery("id");
    layui.http.SendAjax(shopUrl + '/api/addon/detail/', {
        async: 1,
        id: id
    }, {token: localStorage.getItem('user_token')}, function (res) {
        let data = res.data || {};
        layui.http.SendAjax('/common/shop/LocalInfo/', {addon: data.num}, {}, function (res) {
            let local = res.data || {};
            local['install']=local.install || '';
            local['version']=local.version || '';
            data['local'] =local;
            layui.public.ViewTpl(data, 'view_body_data', 'view_body');
            carousel();
            layui.public.layerphotos(".img_con");
        })
    })
}


function carousel() {
    layui.carousel.render({
        elem: '#carousel',
        width: '400px',
        height: '300px',
        interval: 3000
    });
}

function info(id = '') {
    window.location.href = "/common/shop/detail/?id=" + id;
}

function download(id = '') {
    layui.http.SendAjax(shopUrl + '/api/addon/downorder/', {addon_id: id}, {token: localStorage.getItem('user_token')}, function (res) {
        let code = res.code || 203;
        let msg = res.msg || '下载失败';
        switch (Number(code)) {
            case 1://未登录
                layui.public.OpenWindow('/common/shop/userinfo', '会员信息', '500', '500')
                break;
            case 200://确认订单
                layui.public.OpenWindow('/common/shop/confirm/?id=' + res.data.id, '确认订单', '700', '700')
                break
            case 203://其它错误

                break;
        }
    })
}


function Uninstall(addon = '') {

    layer.confirm('确定要卸载吗？', function (index) {
        layer.close(index);
        layui.http.SendAjax('/common/shop/uninstall/', {addon: addon}, {}, function (res) {
            if (res.code == 200) {
                layui.layer.msg('卸载成功', {icon: 1});
                onLoad();
            } else {
                layui.layer.msg(res.msg, {icon: 2});
            }
        })
    })

}

function Install(addon = '') {

    layer.confirm('确定要安装吗？', function (index) {
        layer.close(index);
        layui.http.SendAjax('/common/shop/install/', {addon: addon}, {}, function (res) {
            if (res.code == 200) {
                layui.layer.msg('安装成功', {icon: 1});
                onLoad();
            } else {
                layui.layer.msg(res.msg, {icon: 2});
            }
        })
    })

}

